//
//  WelcomeController.swift
//  CreateMyCode
//
//  Created by zhibao on 2024/2/3.
//

import UIKit
class WelcomeController: UIViewController,SDCycleScrollViewDelegate {
    var contentArray = Array<Dictionary<String, String>>()
    var nextBtn:UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        contentArray.append(
            [
                "title":"二维码师傅",
                "detail":"手机一站式解决二维码制作"
            ]
        )
        contentArray.append(
            [
                "title":"二维码美化",
                "detail":"横版/嵌入Logo/海报"
            ]
        )
        contentArray.append(
            [
                "title":"二维码多样化",
                "detail":"海量模版，任意修改元素"
            ]
        )
        
        let cycleScrollView = SDCycleScrollView(frame: CGRectMake(0, 100, SCREEN_WIDTH, 168), shouldInfiniteLoop: true, imageNamesGroup: ["s","s","s"])
        cycleScrollView?.backgroundColor = UIColor.clear
        cycleScrollView?.infiniteLoop = true
        cycleScrollView?.autoScroll = true
        cycleScrollView?.autoScrollTimeInterval = 4
        cycleScrollView?.bannerImageViewContentMode = .scaleAspectFit
        //cycleScrollView?.pageControlStyle = .pagec
        cycleScrollView?.pageDotColor = MYColor.color(withHexString: "666666", alpha: 0.3)
        cycleScrollView?.currentPageDotColor = MYColor.color(withHexString:"666666")
        cycleScrollView?.pageControlBottomOffset = -10
        self.view.addSubview(cycleScrollView!)
        cycleScrollView?.snp.makeConstraints({ make in
            make.left.right.centerY.equalTo(self.view)
            make.height.equalTo(pixw(p: 168))
        })
        cycleScrollView?.delegate = self
        cycleScrollView?.scrollDirection = .horizontal
        
        let jumpBtn = UIButton(type: .custom)
        jumpBtn.layer.cornerRadius = pixw(p: 19)
        jumpBtn.layer.borderWidth = pixw(p: 0.5)
        jumpBtn.layer.borderColor = MYRed.cgColor
        jumpBtn.addTarget(self, action: #selector(gotoController), for: .touchUpInside)
        jumpBtn.setTitle("    跳过    ", for: .normal)
        jumpBtn.setTitleColor(MYRed, for: .normal)
        jumpBtn.titleLabel?.font = MY_SCALE_RegularFONT(15)
        self.view.addSubview(jumpBtn)
        jumpBtn.snp.makeConstraints { make in
            make.height.equalTo(pixw(p: 38))
            make.right.equalTo(-20)
            make.top.equalTo(statusBarHeight() + pixw(p: 10))
        }
        
        nextBtn = UIButton(type: .custom)
        nextBtn.layer.cornerRadius = pixw(p: 19)
        nextBtn.isHidden = true
        nextBtn.backgroundColor = MYBlue
        nextBtn.addTarget(self, action: #selector(gotoController), for: .touchUpInside)
        nextBtn.setTitle("去往APP", for: .normal)
        nextBtn.setTitleColor(UIColor.white, for: .normal)
        nextBtn.titleLabel?.font = MY_SCALE_RegularFONT(15)
        self.view.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { make in
            make.height.equalTo(pixw(p: 46))
            make.right.equalTo(-50)
            make.left.equalTo(50)
            make.top.equalTo(cycleScrollView!.snp.bottom).offset(30)
        }
    }
    
    @objc func gotoController() {
        UserDefaults.standard.setValue(WelcomeFlag, forKey: WelcomeFlag)
        let tabbarController = TabBarController()
        keyWindow().rootViewController = tabbarController
    }
    
    func keyWindow() -> UIWindow {
           if #available(iOS 15.0, *) {
               let keyWindow = UIApplication.shared.connectedScenes
                   .map({ $0 as? UIWindowScene })
                   .compactMap({ $0 })
                   .first?.windows.first ?? UIWindow()
               return keyWindow
           }else {
               let keyWindow = UIApplication.shared.windows.first ?? UIWindow()
               return keyWindow
           }
       }
    
}

extension WelcomeController{
    func customCollectionViewCellClass(for view: SDCycleScrollView!) -> AnyClass! {
        return UICollectionViewCell.self
    }
    
    func setupCustomCell(_ cell: UICollectionViewCell!, for index: Int, cycleScrollView view: SDCycleScrollView!) {
        for subView in cell.contentView.subviews {
            if subView.tag == 1000 {
                subView.removeFromSuperview()
            }
        }
        
        let titleLabel = UILabel()
        titleLabel.text = contentArray[index]["title"]!
        titleLabel.font = MY_SCALE_SemiboldFONT(28)
        titleLabel.textColor = MYRed
        titleLabel.tag = 1000
        titleLabel.numberOfLines = 0
        titleLabel.lineBreakMode = .byWordWrapping
        titleLabel.textAlignment = .center
        cell.contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.equalTo(pixw(p: 30))
            make.right.equalTo(pixw(p: -30))
        }
        
        let detailLabel = UILabel()
        detailLabel.text = contentArray[index]["detail"]!
        detailLabel.font = MY_SCALE_HeavyFONT(18)
        detailLabel.textColor = MYBlue
        detailLabel.tag = 1000
        detailLabel.numberOfLines = 0
        detailLabel.lineBreakMode = .byWordWrapping
        detailLabel.textAlignment = .center
        cell.contentView.addSubview(detailLabel)
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(pixw(p: 25))
            make.right.left.equalTo(titleLabel)
        }
    }
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didScrollTo index: Int) {
        if index == contentArray.count - 1 {
            nextBtn.isHidden = false
        }else{
            nextBtn.isHidden = true
        }
    }
}
